from django import forms

from web.forms.bootstrap import BootStrapForm
from web import models


class IssuesModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_datetime']
        widgets = {
            "assign": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            "attention": forms.SelectMultiple(
                attrs={'class': "selectpicker", "data-live-search": "true", "data-actions-box": "true"}),
            "parent": forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"})
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 1.问题类型应该是与问题类型表关联的内容
        self.fields['issues_type'].choices = models.IssuesType.objects.filter(
            project=request.tracer.project).values_list("id", "title")

        # 2.模块应该是当前项目的模块选择
        module_list = [('', "没有选中任何项")]
        module_object_list = models.Module.objects.filter(
            project=request.tracer.project).values_list('id', 'title')
        module_list.extend(module_object_list)
        self.fields['module'].choices = module_list

        # 3.指派和关注者应该都是当前项目的创建者和参与者

        total_list = [(request.tracer.project.creator.id, request.tracer.project.creator.username)]

        project_user_list = models.ProjectUser.objects.filter(
            project=request.tracer.project).values_list("user_id", "user__username")
        total_list.extend(project_user_list)

        self.fields['assign'].choices = [("", "没有选中任何项")] + total_list
        self.fields['attention'].choices = total_list

        # 4.父问题应该是当前项目创建的问题
        parent_list_all = [("", "没有选中任何项")]
        parent_list = models.Issues.objects.filter(
            project=request.tracer.project).values_list("id", "subject")
        parent_list_all.extend(parent_list)
        self.fields['parent'].choices = parent_list_all


class IssuesReplyModelForm(forms.ModelForm):
    class Meta:
        model = models.IssuesReply
        fields = ['content', 'reply']


class InviteForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ['period', 'count']
